#!/usr/bin/env bash

[ -z "$GMYSQLDB" ] && GMYSQLDB=pdnstest
[ -z "$GMYSQLUSER" ] && GMYSQLUSER=root
[ -z "$GMYSQLHOST" ] && GMYSQLHOST=localhost
[ -z "$GMYSQLPASSWD" ] && GMYSQLPASSWD=''

step="step.$1"
[ -z "$1" ] && step="start"
comment=$2
[ -z "$comment" ] && comment="No comment"
diffto="step.$3"
[ -z "$3" ] && diffto="start"

if [ "${context: -9}" = "-nodnssec" ]
then
	mysql --user="$GMYSQLUSER" --password="$GMYSQLPASSWD" --host="$GMYSQLHOST" \
		"$GMYSQLDB" \
		-e "SELECT r.name, r.type, r.prio, r.content, r.ttl FROM domains d JOIN records r ON d.id=r.domain_id WHERE d.name='test.dyndns' AND(r.type != 'SOA' OR r.type IS NULL) ORDER BY r.name, r.type, r.content, r.ttl, r.prio" > ${testsdir}/${testname}/$step
else
	mysql --user="$GMYSQLUSER" --password="$GMYSQLPASSWD" --host="$GMYSQLHOST" \
		"$GMYSQLDB" \
		-e "SELECT r.name, r.type, r.prio, r.content, r.ttl, CONCAT('''', CONCAT(r.ordername, '''')), r.auth FROM domains d JOIN records r ON d.id=r.domain_id WHERE d.name='test.dyndns' AND(r.type != 'SOA' OR r.type IS NULL) ORDER BY r.name, r.type, r.content, r.ttl, r.prio" > ${testsdir}/${testname}/$step
fi

if type gsort >/dev/null 2>/dev/null
then
	SORT=gsort
else
	SORT=sort
fi

if [ ! "$step" == "start" ]
then
	echo -e "$comment\n--- Start: diff $diffto $step ---"
	diff ${testsdir}/${testname}/$diffto ${testsdir}/${testname}/$step | sed '/^[0-9a-z,/-]*$/d' | $SORT -V
	if [ ${PIPESTATUS[0]} -eq 0 ]
	then
		echo "no difference"
	fi
	echo -e "--- End: diff $diffto $step ---\n"
fi
